package pl.dsp.dsp1.processing;

public class FirstOrderInterpolator implements Generator {

	private Signal signal;
	
	public FirstOrderInterpolator(Signal signal)
	{
		this.signal = signal;
	}
	
	@Override
	public SignalValue at(double x) {
		double relativePosition = (x - signal.getT1())
				/ signal.getSamplingPeriod();

		int previous = (int) Math.floor(relativePosition);
		int next = (int) Math.ceil(relativePosition);
		double progress = relativePosition - previous;

		if (progress < 0.0001)
			return (SignalValue) signal.get(previous).clone();
		if (progress > 0.9999)
			return (SignalValue) signal.get(next).clone();

		return signal.get(next).substract(signal.get(previous))
				.multiply(new SimpleValue(progress)).add(signal.get(previous));
	}

	@Override
	public boolean isPeriodical() {
		return false;
	}

}
